package com.xxxx.server.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxxx.server.pojo.Menu;
import com.xxxx.server.pojo.MenuRole;
import com.xxxx.server.pojo.ResBean;
import com.xxxx.server.pojo.Role;
import com.xxxx.server.service.IMenuRoleService;
import com.xxxx.server.service.IMenuService;
import com.xxxx.server.service.IRoleService;
import com.xxxx.server.service.impl.RoleServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2021-10-25
 */
@RestController
@RequestMapping("/system/basic/roles")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IMenuRoleService menuRoleService;
    @ApiOperation(value="获取所有角色")
    @GetMapping("/")
    public List<Role> getRoles(){
       return roleService.list();
    }

    @ApiOperation(value="添加角色信息")
    @PostMapping("/")
    public ResBean addRole(@RequestBody Role role){
        if(roleService.save(role))
            return ResBean.success("添加成功！");
        else
            return ResBean.error("添加失败！");
    }
    @ApiOperation(value = "删除角色信息")
    @DeleteMapping("/{id}")
    public ResBean delRole(@PathVariable Integer id){
        if(roleService.removeById(id))
            return ResBean.success("删除成功！");
        else
            return ResBean.error("删除失败！");
    }

    @ApiOperation(value = "批量删除角色信息")
    @DeleteMapping("/")
    public ResBean delRole2(Integer[] ids){
        if(roleService.removeByIds(Arrays.asList(ids)))
        return ResBean.success("删除成功！");
        else
        return ResBean.error("删除失败！");
    }
//    @ApiOperation(value="更新角色信息")
//    @PutMapping("/")
//    public ResBean updateRole(@RequestBody Role role){
//        if(roleService.updateById(role))
//            return ResBean.success("更新成功！");
//        else
//        return ResBean.error("更新失败！");
//    }
    @ApiOperation(value="查询所有菜单")
    @GetMapping("/getMenus")
    public List<Menu> getAllMenus(){
        return menuService.getAllMenus();
    }
    @ApiOperation(value="根据角色id查询菜单id")
    @GetMapping("/getMenusByRoleId/{rid}")
    public List<Integer> getMenusByRoleId(@PathVariable Integer rid){
        return menuRoleService.list(new QueryWrapper<MenuRole>().eq("rid",rid)).stream().map(MenuRole::getMid).collect(Collectors.toList());
    }
    @ApiOperation(value = "更新角色菜单")
    @PutMapping("/")
    public ResBean updateMenuRole(Integer rid ,Integer[] mids){
        return menuRoleService.updateMenuRole(rid,mids);
    }


}
